<?php
/**
 * 举报管理
 * @author chenyangbj8826
 *
 */
class Controller_Admin_Jubao extends Controller_Template{

	/**
	 * 每页显示条数
	 * @var int
	 */
	private $page_per_count = 10; 
	
	public function __construct($request,$response)
	{
		parent::__construct($request,$response);
		$this->template	= ('smarty:admin/jubao/jubao');
	}
	
	/**
	 * 界面展示
	 * @param string $type 要展示举报的类别
	 * @param int $page 页码
	 */
	public function action_index($type='undeal',$page=1)
	{
		//获得举报信息数组
		$offset = ($page -1) * $this->page_per_count;
		$query=array();
		$query['status'] =$type;
		$query['order_by'] = array('create_time' => 'DESC');
		$arr_exposures = $this->get_exposures($query,$offset);
	
		$this->template->set('exposures',$arr_exposures['list']);
		$this->template->set('current_status',$type);
		$this->template->set('arr_status',kohana::config('jubao.status_name'));
		$this->template->set('pagination', Pagination::factory(array('current_page'=> array('source'=> 'route','key'=>'param2'),'total_items'=> $arr_exposures['count'],'items_per_page'=>$this->page_per_count)));
		$this->template->set('pagecount',$arr_exposures['count']);

	}
	
	/**
	 * 调整举报的状态
	 * @param string $action 只有三个值：adjust，removed，ignore
	 * @param string $id 举报id
	 * @param string $query 查询条件
	 */
	public function action_action($action,$id,$query='undeal')
	{
		$explain = $this->request->post('explain');
		$Service_Jubao = new Service_Jubao;
		//设置举报状态
		$Service_Jubao->set_status($id,$action,$explain);
		
		//页面跳转
		$url='/jubao/index/'.$query.'/';
		$this->request->redirect($url,302);
		
	}
	
	/**
	 * 获得举报数组
	 * @param array $query 查询条件数组
	 * @param int $offset 偏移量
	 * @return array 举报数组:
	 */
	private function get_exposures(array $query=array(),$offset)
	{
		$arr_result = Service_Jubao::search($query,$offset,$this->page_per_count);
		// 加工数组
		if(!empty($arr_result) && is_array($arr_result['list']) && count($arr_result['list'])>0)
		{			
			$arr_shops= &$arr_result['list'];
			
			//获得举报人名称
			foreach ($arr_shops as $key=>$value)
			{
				if($arr_shops[$key])
				{
					$arr_shops[$key]['uname'] = User::name($arr_shops[$key]['uid']);
				}
			}
		}
		return $arr_result;
	}

}
